using System;
using System.Collections.Generic;
using System.Text;
using BuildingBasicDomain;

namespace BuildingStructuralElements.Loads
{
    [Serializable ]
    public class NodalLoad:Load
    {
        private Node _node;
        public double[] f;//= new double [6];

        private CommonTypes.Math.Vector3D direction;
        public CommonTypes.Math.Vector3D Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        public NodalLoad(IPoint  _node)
        {
            SetUpNode(_node);
        }
        private void SetUpNode(IPoint _node)
        {
            try
            {
                if (_node is Node)
                    this._node = _node as Node;
                else
                    this._node = (Node)_node;
            }
            catch { throw new Exception("NodalLoad must be Applied to Node."); }
        }

        public NodalLoad()
        {
            this.Node = null;
        }

        public NodalLoad(double fx, double fy, double fz, double mx, double my, double mz)
        {
            this.f = new double[6];
            this.Fx = fx;
            this.Fy = fy;
            this.Fz = fz;
            this.Mx = mx;
            this.My = my;
            this.Mz = mz;
        }
        public NodalLoad(double fx, double fy, double fz, double mx, double my, double mz, IPoint node) : this(fx, fy, fz, mz, my, mz) { SetUpNode( node); }

        public NodalLoad(double[] load) : this(load[0], load[1], load[2], load[3], load[4], load[5]) { }
        public NodalLoad(double[] load, IPoint node) 
        {
            Fx = load[0];
            Fy = load[1];
            Fz = load[2];
            Mx = load[3];
            My = load[4];
            Mz = load[5];

            SetUpNode(node); 
        }
        public  NodalLoad(NodalLoad load )
        {
            this.f = load.f;
            this.direction = load.direction;

            if (load.Node != null)
                this.Node = new Node(load.Node.X, load.Node.Y, load.Node.Z);

        }
        public enum LoadType {Forces,Moments,};
        
        public NodalLoad(double intensity, CommonTypes.Math.Vector3D direction,LoadType loadType)
        {
            f = new double[6];
            CommonTypes.Math.Vector3D vect = intensity * direction;
            if (loadType == LoadType.Forces)
            {
                f[0] = vect[0];
                f[1] = vect[1];
                f[2] = vect[2];
            }
            else if (loadType == LoadType.Moments)
            {
                f[3] = vect[3];
                f[4] = vect[4];
                f[5] = vect[5];
            }
        }

        public double this[int i]
        {
            get
            {
                if (i < 6 && i >= 0)
                    return f[i];
                else
                    return -1;
            }
            set
            {
                if (i < 6 && i >= 0)
                    f[i] = value;
                else
                    return;
            }
        }
        public Node Node
        {
            get { return _node; }
            set { _node = value; }
        }

        public double Fx
        {
            get {return this.f[0] ;}
            set { this.f[0] = value;}
        }

        public double Fy
        {
            get { return this.f[1]; }
            set { this.f[1] = value; }
        }

        public double Fz
        {
            get { return this.f[2]; }
            set { this.f[2] = value; }
        }

        public double Mx
        {
            get { return this.f[3]; }
            set { this.f[3] = value; }
        }

        public double My
        {
            get { return this.f[4]; }
            set { this.f[4] = value; }
        }

        public double Mz
        {
            get { return this.f[5]; }
            set { this.f[5] = value; }
        }

        public  override Load[] Distribute()
        {
            return null;
        }

        public override double TotalLoad(int direction)
        {
            return f[direction];
        }
    }
}
